package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.lewin.movie.entity.MovieScene;
import la.iok.hzsvn.lewin.movie.entity.MovieSceneInstance;
import la.iok.hzsvn.lewin.movie.mapper.MovieSceneInstanceMapper;
import la.iok.hzsvn.lewin.movie.model.MovieSceneVo;
import la.iok.hzsvn.lewin.mybatis.service.BaseServiceImpl;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.NotNullElement;
import la.iok.hzsvn.share.annotations.NullElement;
import la.iok.hzsvn.share.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MovieSceneInstanceServiceImpl extends BaseServiceImpl<MovieSceneInstance, MovieSceneInstanceMapper,Long> implements MovieSceneInstanceService{
    public MovieSceneInstanceServiceImpl(MovieSceneInstanceMapper mapper) {
        super(mapper);
    }

    @Nullable
    @Override
    public MovieSceneInstance select(@NotNull Long movieId, @NotNull Long fileId) {
        return mapper.selectMovieFileInst(movieId,fileId);
    }

    @Nullable
    @Override
    public MovieSceneInstance movieScene(@Nullable Long movieInstanceId,@Nullable Long movieSceneId) {
        if(movieInstanceId == null || movieSceneId == null){
            return null;
        }
        return mapper.selectMovieScene(movieInstanceId,movieSceneId);
    }

    @NotNull
    @Override
    public Long save(@NotNull MovieScene movieScene,@NotNull Long movieInstanceId,@NotNull Long encodedFileId) {
        MovieSceneInstance instance = new MovieSceneInstance();
        BeanUtils.copyProperties(movieScene,instance);
        instance.setId(null);
        instance.setMovieSceneId(movieScene.getId());
        instance.setMovieInstanceId(movieInstanceId);
        instance.setEncodedFileId(encodedFileId);
        insertSelective(instance);
        return instance.getId();
    }

    @NotNull
    @NotNullElement
    @Override
    public List<MovieSceneVo> po2vo(@NotNull @NotNullElement List<MovieSceneInstance> pos) {
        return pos.stream().map(this::po2vo).collect(Collectors.toList());
    }

    @Override
    public void markFileDeleted(@Nullable @NullElement Collection<Long> ids) {
        batchUpdate("fileDeleted",1,ids);
    }

    private MovieSceneVo po2vo(MovieSceneInstance po){
        MovieSceneVo vo = new MovieSceneVo();
        BeanUtils.copyProperties(po,vo);
        vo.setId(po.getMovieSceneId());
        return vo;
    }
}
